<template>
  <view class="textCenter">
    <button class="getPhoneBtn" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">微信授权</button>
  </view>
</template>

<script>
import { sendInfo, getUserPatientByUnionId } from '@/api/join.js';
import { tongji } from '@/utils/tools.js';
export default {
  data() {
    return {};
  },
  computed: {
    sessionKey() {
      return this.$store.getters.sessionKey;
    }
  },
  methods: {
    bindGetUserInfo(e) {
      // 微信授权
      tongji('cf570a1f-c110-4304-8d92-32ee40f677ce');
      const self = this;
      wx.getUserInfo({
        lang: 'zh_CN',
        success: (res) => {
          console.log('用户信息-----------', res);
          const infoData = {
            appid: self.$store.getters.appid,
            sessionKey: this.sessionKey,
            signature: res.signature,
            rawData: res.rawData,
            encryptedData: res.encryptedData,
            iv: res.iv
          };
          self.$store.commit('join/SET_RAWDATA', res.rawData);
          self.$store.commit('join/SET_SIGNATURE', res.signature);
          sendInfo(infoData).then((userInfo) => {
            if (userInfo.code === 0) {
              // self.$store.commit('join/SET_UNIONID', userInfo.data.unionId);
              // self.ifGoHome(userInfo.data.unionId);
            }
          });
          self.$emit('authorOk');
        },
        fail: (e) => {
          console.log(e);
          wx.hideLoading();
          wx.showToast({
            title: '拉取用户信息失败，请授权',
            icon: 'none',
            duration: 2000
          });
        }
      });
    },
    async ifGoHome(val) {
      const { data: patientId } = await getUserPatientByUnionId(val);
      if (patientId) {
        this.$store.commit('join/SET_PATIENTID', patientId);
        uni.navigateTo({
          url: '/pages/index/index'
        });
      }
    }
  }
};
</script>

<style lang="scss">
.textCenter {
  text-align: center;
}
.getPhoneBtn {
  color: #fff;
  width: 700rpx;
  height: 88rpx;
  line-height: 88rpx;
  border-radius: 44rpx;
  font-size: 36rpx;
  background: -moz-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fe894d), color-stop(100%, #ef3931));
  background: -webkit-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: -o-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: -ms-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: linear-gradient(to bottom, #fe894d 0%, #ef3931 100%);
}
</style>
